package cn.aspart.async;

import cn.aspart.async.service.CalculateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableAsync;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;

@Slf4j
@EnableAsync
@SpringBootApplication
public class AsyncApplication implements CommandLineRunner {

    public static void main(String[] args) {
        SpringApplication.run(AsyncApplication.class, args);
    }

    @Autowired
    private CalculateService calculateService;

    @Override
    public void run(String... args) throws Exception {
        Integer a = 5, b = 6;

        CompletableFuture<Integer> addFuture = calculateService.add(a, b);

        addFuture.thenApply(i -> calculateService.sub(a, b))
                .thenApply(i -> calculateService.sum(a, b));


//            Future<Integer> subFuture = calculateService.sub(a, b);

//        int c = calculateService.sum(a, b);
//        Integer d = addFuture.get(5, TimeUnit.SECONDS);
//            Integer e = subFuture.get(5, TimeUnit.SECONDS);

//            log.info("value c is {}, d is {}, e is {}", c, d, e);
//        log.info("value c is {}, d is {}", c, d);
    }
}
